package com.dm.library.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.dm.library.R;
import com.dm.library.utils.DensityUtil;
import com.dm.library.widgets.element.DTextView;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * Created by sushuo on 2017/02/23.
 */

public class OneLineEditLinearLayout extends LinearLayout {

    private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#ffa9a9a9");
    private static final int DEFAULT_TEXT_SIZE = 14;// sp
    private static final boolean IS_SHOW_BOTTOM_LINE = true;

    private DTextView tvTextLeft;
    private EditText etTextRight;
    private View vBottomLine;
    private String rightTextContent;
    private String leftTextContent;
    private TextWatcher textWatcher;
    private int flag;


    public OneLineEditLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public OneLineEditLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public OneLineEditLinearLayout(Context context) {
        super(context);
        initView(context, null);
    }

    private void initView(Context context, AttributeSet attrs) {
        // attachToRoot传入true代表layout文件填充的View会被直接添加进ViewGroup，而传入false则代表创建的View会以其他方式被添加进ViewGroup。
        View view = LayoutInflater.from(context).inflate(R.layout.one_line_edit_layout, this);// 相当于attachToRoot为true
        tvTextLeft = (DTextView) view.findViewById(R.id.tv_text_left);
        etTextRight = (EditText) view.findViewById(R.id.et_text_right);
        vBottomLine = view.findViewById(R.id.v_bottom_line);

        if (null != attrs && !isInEditMode()) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OneLineEditLinearLayout);
            boolean isShowBottomLine = typedArray.getBoolean(R.styleable.OneLineEditLinearLayout_isShowBottomLineEdit, IS_SHOW_BOTTOM_LINE);

            int rightTextColor = typedArray.getColor(R.styleable.OneLineEditLinearLayout_rightTextColorEdit, DEFAULT_TEXT_COLOR);
            int rightTextSize = typedArray.getDimensionPixelSize(R.styleable.OneLineEditLinearLayout_rightTextSizeEdit, DensityUtil.sp2px(context, DEFAULT_TEXT_SIZE));
            rightTextContent = typedArray.getString(R.styleable.OneLineEditLinearLayout_rightTextContentEdit);

            int rightTextHintColor = typedArray.getColor(R.styleable.OneLineEditLinearLayout_rightTextHintColorEdit, DEFAULT_TEXT_COLOR);
            String rightTextHintContent = typedArray.getString(R.styleable.OneLineEditLinearLayout_rightTextHintContentEdit);

            int leftTextSize = typedArray.getDimensionPixelSize(R.styleable.OneLineEditLinearLayout_leftTextSizeEdit, DensityUtil.sp2px(context, DEFAULT_TEXT_SIZE));
            int leftTextColor = typedArray.getColor(R.styleable.OneLineEditLinearLayout_leftTextColorEdit, DEFAULT_TEXT_COLOR);
            leftTextContent = typedArray.getString(R.styleable.OneLineEditLinearLayout_leftTextContentEdit);


            setLeftContent(leftTextContent);
            tvTextLeft.setTextColor(leftTextColor);
            tvTextLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize);

            setRightContent(rightTextContent);
            etTextRight.setHint(null != rightTextHintContent ? rightTextHintContent : "");
            etTextRight.setTextColor(rightTextColor);
            etTextRight.setHintTextColor(rightTextHintColor);
            etTextRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize);

            etTextRight.addTextChangedListener(new BaseTextWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    notifyTextWatcher(s, flag);
                }
            });

            vBottomLine.setVisibility(isShowBottomLine ? VISIBLE : GONE);
            typedArray.recycle();
        }
    }


    /***
     * 设置下划线显示隐藏
     */
    public void isShowBottomLine(boolean showBottomLine) {
        vBottomLine.setVisibility(showBottomLine ? VISIBLE : GONE);
    }


    /***
     * 设置左边文字大小
     */
    public void setLeftTextSize(float size) {
        tvTextLeft.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    /***
     * 设置右边文字大小
     */
    public void setRightTextSize(float size) {
        etTextRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }


    /***
     * 设置左边文字
     */
    public void setLeftContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            this.leftTextContent = content;
            tvTextLeft.setTextContent(content);
        }
    }


    /***
     * 设置右边文字
     */
    public void setRightContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            this.rightTextContent = content;
            etTextRight.setText(content);
        }
    }

    public void setMaxLength(int len) {
        etTextRight.setFilters(new InputFilter[]{new InputFilter.LengthFilter(len)});
    }


    /***
     * 设置右边文字颜色
     */
    public void setRightTextColor(int color) {
        if (null != etTextRight) {
            etTextRight.setTextColor(getResources().getColor(color));
        }
    }

    /***
     * 设置左边文字颜色
     */
    public void setLeftTextColor(int color) {
        if (null != tvTextLeft) {
            tvTextLeft.setTextColor(getResources().getColor(color));
        }
    }

    public String getRightText() {
        return rightTextContent == null?"":rightTextContent;
    }

    public String getLeftText() {
        return leftTextContent;
    }

    public void setInputType(int type) {
        etTextRight.setInputType(type);
    }

    public void setTextWatcher(TextWatcher textWatcher) {
        this.textWatcher = textWatcher;
    }

    public void setTextWatcher(TextWatcher textWatcher, int flag) {
        this.textWatcher = textWatcher;
        this.flag = flag;
    }


    public void notifyTextWatcher(Editable s, int flag) {
        if (null != textWatcher) {
            textWatcher.afterTextChanged(s, flag);
        }
    }

    public interface TextWatcher {
        void afterTextChanged(Editable s, int flag);
    }
}


